<template>
  <div class="login-content">
    <div class="form">
      <!-- 登录框 -->
      <div class="login" v-show="formShow">
        <h2>管理人员登录</h2>
        <Form :model="loginUser" @submit.native.prevent ref="loginFormValidate" :rules="loginRuleValidate">
          <FormItem prop="name">
              <Input type="text" v-model="loginUser.name" placeholder="管理员账号"/>
          </FormItem>
          <FormItem prop="password">
              <Input type="password" v-model="loginUser.password" placeholder="密码"/>
          </FormItem>
          <FormItem>
            <Button type="success" long @click="loingInfo">登录</Button>
          </FormItem>
        </Form>
        <!-- 图标 -->
        <i class="iconfont icon-zhuce" @click="switchIcon"></i>
      </div>
      <!-- 注册框 -->
      <div class="register" v-show="!formShow">
        <h2>注册</h2>
        <Form :model="registerUser" @submit.native.prevent ref="registerFormValidate" :rules="registerRuleValidate">
          <FormItem prop="name">
              <Input type="text" v-model="registerUser.name" placeholder="管理员账号" @blur.native.capture="selectUser"/>
          </FormItem>
          <FormItem prop="password">
              <Input type="password" v-model="registerUser.password" placeholder="密码"/>
          </FormItem>
          <FormItem>
            <Button type="warning" long @click="registerInfo">注册</Button>
          </FormItem>
        </Form>
        <!-- 图标 -->
        <i class="iconfont icon-denglu" @click="switchIcon"></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登录数据
      loginUser: {
        name: '',
        password: '',
      },
      // 注册数据
      registerUser: {
        name: '',
        password: '',
      },
      // 登录框的表单验证规则
      loginRuleValidate: {
        name: { required: true, message: '请输入管理员账号', trigger: 'blur' },
        password: { required: true, message: '请输入账号密码', trigger: 'blur' }
      },
      // 注册框的表单验证规则
      registerRuleValidate: {
        name: { required: true, message: '请输入管理员账号', trigger: 'blur' },
        password: { required: true, message: '密码长度6~12', min: 6, max: 12, trigger: 'blur' }
      },
      // 判断当前显示的框
      formShow: true
    }
  },
  methods: {
    // 切换图片事件
    switchIcon () {
      this.formShow = !this.formShow
      // 重置表单样式
      this.formShow ? this.$refs.loginFormValidate.resetFields() : this.$refs.registerFormValidate.resetFields()
    },
    // 验证用户名是否存在
    async selectUser () {
      // 获取当前输入的名称，发起请求
      const { name } = this.registerUser

      await this.$http.get(`/selectUser/${name}`)

    },
    // 登录按钮
    async loingInfo () {
      const { data, status } = await this.$http.post('/login', this.loginUser)

      // 登录成功，跳转到主页，提示登录成功，存储 token
      if (status === 200) {
        // 存储token
        localStorage.setItem('token', data.token)

        // 跳转主页面
        this.$router.push('/')

        this.$message.success({
          background: true,
          content: `欢迎登录 ${data.name}`
        })
      }
    },
    // 注册按钮
    async registerInfo () {
      // 发送请求
      const { data } = await this.$http.post('/register', this.registerUser)

      // 注册成功，切换到登录框，并提示进行登录操作
      if (data.status === 200) {
        this.formShow = true
        this.$message.success({
          background: true,
          content: `${data.message},请进行登录`
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
 .login-content {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: url(https://cn.bing.com/th?id=OHR.VogelhaeuserD_ZH-CN8437589222_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp);
  background-size: 100% 100%;
  background-position: top center;
  // 登录表单框
  .form {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 500px;
    height: 350px;
    background: inherit;
    overflow: hidden;
    z-index: 5;
    color: #333;
    text-align: center;
    border-radius: 10px;
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: inherit;
      box-shadow: 10px 0 12px 400px rgba(255, 255, 255, 0.548) inset;
      filter: blur(3px);
      z-index: -1;
    }
    // 登录注册框混合器
    @mixin log_reg{
      display: flex;
      flex-direction: column;
      align-content: center;
      justify-content: space-around;

      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;

      h2 {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 30px;
        font-weight: 400;
        letter-spacing: 4px;
      }
      Form {
        flex: 1.8;
        display: flex;
        flex-direction: column;
        align-items: center;
        .ivu-form-item {
          width: 50%;
        }
        Button {
          margin-top: 5px;
          letter-spacing: 2px;
        }
      }
      // 图标
      i {
        position: absolute;
        right: 20px;
        bottom: 20px;
        font-size: 25px;
        cursor: pointer;
      }
    }
    // 登录框
    .login {
      @include log_reg;
    }
    // 注册框
    .register {
      @include log_reg;
    }
  }
}
</style>
